{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**334. Increasing Triplet Subsequence**  <font color=#f0ad4e size=3>_medium_</font>\n",
    "\n",
    "Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.   \n",
    "Formally the function should:\n",
    "\n",
    ">Return true if there exists i, j, k \n",
    "such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.\n",
    "\n",
    "**Note:** Your algorithm should run in O(n) time complexity and O(1) space complexity.\n",
    "\n",
    "**Example 1**\n",
    "\n",
    "```\n",
    "Input: [1,2,3,4,5]\n",
    "Output: true\n",
    "```\n",
    "\n",
    "**Example 2**\n",
    "\n",
    "```\n",
    "Input: [5,4,3,2,1]\n",
    "Output: false\n",
    "```\n",
    "**Follow up:**\n",
    "What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "def increasingTriplet(nums):\n",
    "    \"\"\"\n",
    "    :type nums: List[int]\n",
    "    :rtype: bool\n",
    "    \"\"\"\n",
    "    seq = [nums[0]]\n",
    "    for i in range(1, len(nums)):\n",
    "        if nums[i] <= nums[i - 1]:\n",
    "            seq[-1] = nums[i]\n",
    "        else:\n",
    "            seq.append(nums[i])\n",
    "            if len(seq) >= 3: return True\n",
    "    return False\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(increasingTriplet([1, 2, 3, 4, 5]))\n",
    "    print(increasingTriplet([5, 4, 3, 2, 1]))\n",
    "    print(increasingTriplet([3, 1, 0, 5, 2, 4]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
